//
//  MineFuncVM.swift
//  kxf
//
//  Created by Mac on 2024/3/5.
//

import Foundation
import UIKit

class MineFuncVM: UIView {
    
    var selfHeight = MineFuncItemVM().selfHeight*11

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: kFitWidth(16), y: frame.origin.y, width: SCREEN_WIDHT-kFitWidth(32), height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        self.layer.cornerRadius = kFitWidth(14)
        self.clipsToBounds = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var qrCodeVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "我的码牌"
        return vm
    }()
    lazy var mercMsgVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.qrCodeVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "商户信息"
        return vm
    }()
    lazy var bankCardVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.mercMsgVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "结算卡号"
        return vm
    }()
    lazy var tradeRecordsVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.bankCardVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "交易记录"
        return vm
    }()
    lazy var settleVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.tradeRecordsVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "结算管理"
        return vm
    }()
    lazy var mercFeeVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.settleVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "商户费率"
        return vm
    }()
    lazy var helpCenterVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.mercFeeVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "帮助中心"
        return vm
    }()
    lazy var mercManageVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.helpCenterVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "店铺管理"
        return vm
    }()
    lazy var authoriVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.mercManageVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "商户授权"
        //posStatus >= 2
        return vm
    }()
    lazy var signVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.authoriVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "商户签约"
        //
        return vm
    }()
    lazy var settingVm : MineFuncItemVM = {
        let vm = MineFuncItemVM.init(frame: CGRect.init(x: 0, y: self.signVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "我的设置"
        vm.lineView.isHidden = true
        return vm
    }()
}

extension MineFuncVM{
    func refreshUI() {
        if UserModel.shared.isCheck == true{
            return
        }
        let frame = self.frame
        
        var settingCenterY = MineFuncItemVM().selfHeight*7.5
        var authorCenterY = kFitWidth(0)
        var signCenterY = kFitWidth(0)
        
        //如果要显示商户费率
        if UserModel.shared.isShowRate == "Y"{
            self.mercFeeVm.isHidden = false
            selfHeight = MineFuncItemVM().selfHeight*9
            self.helpCenterVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: MineFuncItemVM().selfHeight*6.5)
            self.mercManageVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: MineFuncItemVM().selfHeight*7.5)
            
            settingCenterY = settingCenterY + MineFuncItemVM().selfHeight
            authorCenterY = MineFuncItemVM().selfHeight*8.5
            signCenterY = MineFuncItemVM().selfHeight*8.5
        }else{
            self.mercFeeVm.isHidden = true
            selfHeight = MineFuncItemVM().selfHeight*8
            self.helpCenterVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: MineFuncItemVM().selfHeight*5.5)
            self.mercManageVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: MineFuncItemVM().selfHeight*6.5)
            authorCenterY = MineFuncItemVM().selfHeight*7.5
            signCenterY = MineFuncItemVM().selfHeight*7.5
        }
        
        //如果要显示商户授权
        if UserModel.shared.posStatus == "2" || UserModel.shared.posStatus == "3" || UserModel.shared.posStatus == "4"{
            self.authoriVm.isHidden = false
            self.authoriVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: authorCenterY)
            selfHeight = selfHeight + MineFuncItemVM().selfHeight
            settingCenterY = settingCenterY + MineFuncItemVM().selfHeight
            signCenterY = signCenterY + MineFuncItemVM().selfHeight
        }else{
            self.authoriVm.isHidden = true
        }
        
        //如果要显示商户签约
        if UserModel.shared.posStatus == "1" || UserModel.shared.posStatus == "2" || UserModel.shared.posStatus == "3" || UserModel.shared.posStatus == "4"{
            if UserModel.shared.signType != "" && UserModel.shared.signType != "1" && UserModel.shared.signType != "2"{
                signVm.isHidden = false
                signVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: signCenterY)
                
                selfHeight = selfHeight + MineFuncItemVM().selfHeight
                settingCenterY = settingCenterY + MineFuncItemVM().selfHeight
            }else{
                signVm.isHidden = true
            }
        }else{
            signVm.isHidden = true
        }
        
        self.settingVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: settingCenterY)
        self.frame = CGRect.init(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: selfHeight)
    }
}

extension MineFuncVM{
    func initUI()  {
        addSubview(qrCodeVm)
        addSubview(mercMsgVm)
        addSubview(bankCardVm)
        addSubview(tradeRecordsVm)
        addSubview(helpCenterVm)
        addSubview(settingVm)
        
        if UserModel.shared.isCheck == false{
            addSubview(settleVm)
            addSubview(mercFeeVm)
            addSubview(mercManageVm)
            addSubview(authoriVm)
            addSubview(signVm)
        }else{
            helpCenterVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: MineFuncItemVM().selfHeight*4.5)
            settingVm.center = CGPoint.init(x: MineFuncItemVM().selfWidth*0.5, y: MineFuncItemVM().selfHeight*5.5)
            selfHeight = MineFuncItemVM().selfHeight*6
        }
    }
}
